package academic.emailClient.utils;

import java.io.File;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import academic.emailClient.persistence.TemplateFileDAO;

public class TemplateFormatter {
	
	private static String VARIABLE_START = "\\$\\{";
	private static String VARIABLE_END = "\\}";
	private static String EMPTY = "";
	
	/**
	 * Format a text given the variables to be replaced in the text
	 * @param template Text that is going to be formatted
	 * @param variables Variables that are going to be replaced
	 * @return Formated text
	 */
	public static String format(String template, HashMap<String, String> variables){
		if (template.equals(EMPTY))
			return template;
		
		for (String variableName : variables.keySet()){
			String variable = VARIABLE_START  + variableName + VARIABLE_END;
			Pattern variablePattern = Pattern.compile(variable);
			Matcher matcher = variablePattern.matcher(template);
			template = matcher.replaceAll(variables.get(variableName));
			
		}
		return template;
	}
	
}
